const { departModel } = require("../model")

//添加科室
exports.addDepartment = async (req,res)=>{
    const { DepartName,DepartContent  } = req.body
    const dbBack = new departModel({
        DepartName,
        DepartContent
    })
    let result = await departModel.findOne({DepartName})
    if(result) {
        return res.status(400).json({
          code:400,
          message:'该科室已存在',
          data:null  
        })
    }
    try {
        await dbBack.save()
        res.status(200).json({
            code:200,
            message:'添加成功',
            data:null
        })
    } catch (error) {
        res.status(500).json({
            code:500,
            message:'服务器出现问题',
            data:null
        })
    }
}
//添加二级科室
exports.addDepartment2 = async (req, res) => {
    const { tweName } = req.body;
    console.log(tweName);
    const id = req.query.id;

    // 确保找到相应的科室
    const result = await departModel.findOne({ _id: id });
    if (!result) {
        return res.status(404).json({
            code: 404,
            message: "该科室不存在",
            data: null,
        });
    }

    // 检查科室是否已存在
    if (result.TwoDepart.includes(tweName)) {
        return res.status(400).json({
            code: 400,
            message: "该科室已存在",
            data: null,
        });r
    }

    // 确保变量声明，避免潜在的 ReferenceError
    result.TwoDepart.push(tweName);

    try {
        // 保存修改
        await result.save();
        res.status(200).json({
            code: 200,
            message: "添加成功",
            data: null,
        });
    } catch (error) {
        // 处理异常
        res.status(500).json({
            code: 500,
            message: "服务器出现问题",
            data: error,
        });
    }
};

//获取科室列表
exports.getDepartmentList = async (req,res)=>{
    const {pageNo=1,pageSize=10} = req.query
    try {
        let result = await departModel.find().skip((pageNo-1)*pageSize).limit(pageSize)
        let total = await (await departModel.find()).length
        res.status(200).json({
            code:200,
            message:'获取成功',
            data:result,
            total:total
        })
    } catch (error) {
        res.status(500).json({
            code:500,
            message:'获取失败',
            data:error
        })
    }
}
//删除科室
exports.deleteDepartment = async (req,res)=>{
    const id = req.query.id
    try {
        await departModel.deleteOne({_id:id})
        res.status(200).json({
            code:200,
            message:'删除成功',
            data:null
        })
    } catch (error) {
        res.status(500).json({
            code:500,
            message:'删除失败',
            data:null
        })
    }
}
//修改科室
exports.updateDepartment = async (req,res)=>{
    const id = req.query.id
    const {DepartName , DepartContent , TwoDepart} = req.body
    //判断TwoDepart数组中是否有重复的数据
    const isRepeat = TwoDepart.some((item,index)=>{
        return TwoDepart.indexOf(item) != index
    })
    if(isRepeat) {
        return res.status(500).json({
          code:500,
          message:"科室名称不能重复",
          data:null
        })
    }
    try {
        //修改
        await departModel.updateOne({_id:id},{
            DepartName:DepartName,
            DepartContent:DepartContent,
            TwoDepart:TwoDepart
        })
        res.status(200).json({
            code:200,
            message:'修改成功',
            data:null
        })
    } catch (error) {
        res.status(500).json({
            code:500,
            message:'修改失败',
            data:error
        })
    }
}
//关键字搜索
exports.searchDepartment = async (req,res)=>{
    const {keyword} = req.query
    try {
        let result = await departModel.find({DepartName:{$regex:keyword}})
        let total = (await departModel.find({DepartName:{$regex:keyword}})).length
        res.status(200).json({
            code:200,
            message:'查询成功',
            data:result,
            total:total
        })
    } catch (error) {
        res.status(500).json({
            code:500,
            message:'查询失败',
            data:error
        })
    }
}